//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

use prost::Message;
use proto_gen_rust::google::protobuf::Any;
use proto_gen_rust::{board_draw_type, TypeUrl};

#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Debug)]
pub enum NodeContent {
    UnkwownContent(board_draw_type::UnkwownContent),
    TextContent(board_draw_type::TextContent),

    BasicRectangleContent(board_draw_type::BasicRectangleContent),
    BasicEllipseContent(board_draw_type::BasicEllipseContent),
    BasicSvgContent(board_draw_type::BasicSvgContent),
    BasicImageContent(board_draw_type::BasicImageContent),
    BasicLinkContent(board_draw_type::BasicLinkContent),

    LlmAskContent(board_draw_type::LlmAskContent),
    LlmAnswerContent(board_draw_type::LlmAnswerContent),

    DevelopIssueContent(board_draw_type::DevelopIssueContent),
    DevelopMermaidContent(board_draw_type::DevelopMermaidContent),

}

pub fn decode(data: &Any) -> NodeContent {
    if data.type_url == board_draw_type::UnkwownContent::type_url() {
        if let Ok(content) = board_draw_type::UnkwownContent::decode(data.value.as_slice()) {
            return NodeContent::UnkwownContent(content);
        }
    } else if data.type_url == board_draw_type::TextContent::type_url() {
        if let Ok(content) = board_draw_type::TextContent::decode(data.value.as_slice()) {
            return NodeContent::TextContent(content);
        }
    } else if data.type_url == board_draw_type::BasicRectangleContent::type_url() {
        if let Ok(content) = board_draw_type::BasicRectangleContent::decode(data.value.as_slice()) {
            return NodeContent::BasicRectangleContent(content);
        }
    } else if data.type_url == board_draw_type::BasicEllipseContent::type_url() {
        if let Ok(content) = board_draw_type::BasicEllipseContent::decode(data.value.as_slice()) {
            return NodeContent::BasicEllipseContent(content);
        }
    } else if data.type_url == board_draw_type::BasicSvgContent::type_url() {
        if let Ok(content) = board_draw_type::BasicSvgContent::decode(data.value.as_slice()) {
            return NodeContent::BasicSvgContent(content);
        }
    } else if data.type_url == board_draw_type::BasicImageContent::type_url() {
        if let Ok(content) = board_draw_type::BasicImageContent::decode(data.value.as_slice()) {
            return NodeContent::BasicImageContent(content);
        }
    } else if data.type_url == board_draw_type::BasicLinkContent::type_url() {
        if let Ok(content) = board_draw_type::BasicLinkContent::decode(data.value.as_slice()) {
            return NodeContent::BasicLinkContent(content);
        }
    } else if data.type_url == board_draw_type::LlmAskContent::type_url() {
        if let Ok(content) = board_draw_type::LlmAskContent::decode(data.value.as_slice()) {
            return NodeContent::LlmAskContent(content);
        }
    } else if data.type_url == board_draw_type::LlmAnswerContent::type_url() {
        if let Ok(content) = board_draw_type::LlmAnswerContent::decode(data.value.as_slice()) {
            return NodeContent::LlmAnswerContent(content);
        }
    } else if data.type_url == board_draw_type::DevelopIssueContent::type_url() {
        if let Ok(content) = board_draw_type::DevelopIssueContent::decode(data.value.as_slice()) {
            return NodeContent::DevelopIssueContent(content);
        }
    } else if data.type_url == board_draw_type::DevelopMermaidContent::type_url() {
        if let Ok(content) = board_draw_type::DevelopMermaidContent::decode(data.value.as_slice()) {
            return NodeContent::DevelopMermaidContent(content);
        }
    } 
    return NodeContent::UnkwownContent(board_draw_type::UnkwownContent {});
}

pub fn encode(node_content: &NodeContent) -> Result<Any, String> {
    let mut data: Vec<u8> = Vec::new();
    match node_content {
        NodeContent::UnkwownContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::UnkwownContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::TextContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::TextContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::BasicRectangleContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::BasicRectangleContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::BasicEllipseContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::BasicEllipseContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::BasicSvgContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::BasicSvgContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::BasicImageContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::BasicImageContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::BasicLinkContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::BasicLinkContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::LlmAskContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::LlmAskContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::LlmAnswerContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::LlmAnswerContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::DevelopIssueContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::DevelopIssueContent::type_url().into(),
                value: data,
            });
        }
        NodeContent::DevelopMermaidContent(content) => {
            let res = content.encode(&mut data);
            if res.is_err() {
                return Err(res.err().unwrap().to_string());
            }
            return Ok(Any {
                type_url: board_draw_type::DevelopMermaidContent::type_url().into(),
                value: data,
            });
        }
    }
}
